package com.beiing.tianshuai.tianshuai.message.model;

import com.beiing.tianshuai.tianshuai.entity.SearchFriendBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/8/16 0016.
 */

public class SearchFriendModel implements SearchFriendModelImpl{

    private static final String         TAG = "SearchFriendModel";
    private OnRequestListener           mListener;

    public SearchFriendModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getSearchFriendResult(String uid, String keyWords) {
        Observable<SearchFriendBean> observable = DataManager.getSearchFriendResult(
                uid,
                keyWords
        );
        Observable<SearchFriendBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<SearchFriendBean>() {

            @Override
            public void onNext(@NonNull SearchFriendBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {
        void onSuccess(SearchFriendBean bean);
        void onError(Throwable throwable);
    }
}
